package com.qdxht.dispenser.view;

import android.content.Context;
import android.content.DialogInterface;
import android.view.WindowManager;
import androidx.appcompat.app.AlertDialog;

import com.qdxht.dispenser.R;
import com.qdxht.dispenser.utils.Tools;

/**
 * Created by SEA on 16/9/18.
 * 单例弹框，下一次调用会主动关闭上一个弹框
 */
public class DialogSingleUtil {
    private static DialogSingleUtil dialogNoNetUtil;

    private DialogSingleUtil() {
    }

    public static synchronized DialogSingleUtil getInstance() {
        if (dialogNoNetUtil == null) {
            dialogNoNetUtil = new DialogSingleUtil();
        }
        return dialogNoNetUtil;
    }

    private AlertDialog noNetDialog;

    /**
     * 显示无网络对话框
     * @param context
     */
    public void showNoNetWork(final Context context) {
        if(noNetDialog !=null){
            try{
                noNetDialog.cancel();
            }catch (Exception e){
                e.printStackTrace();
            }
            noNetDialog = null;
        }

        noNetDialog = new AlertDialog.Builder(context).create();
        noNetDialog.setTitle(R.string.dialog_title);
        noNetDialog.setMessage(context.getResources().getString(R.string.no_network));
        noNetDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.set_network), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Tools.toSystemSetting(context);
            }
        });
        noNetDialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        noNetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                noNetDialog = null;
            }
        });

        try {
            noNetDialog.show();
        } catch (WindowManager.BadTokenException e) {
            e.printStackTrace();
        }

    }

    private AlertDialog serverDialog;

    /**
     * 显示服务器错误dialog
     * @param context
     */
    public void showServerError(final Context context) {
        if(serverDialog !=null){
            try{
                serverDialog.cancel();
            }catch (Exception e){
                e.printStackTrace();
            }
            serverDialog = null;
        }

        serverDialog = new AlertDialog.Builder(context).create();
        serverDialog.setTitle(R.string.dialog_title);
        serverDialog.setMessage(context.getResources().getString(R.string.the_server_no_respond));
//        serverDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.set_network), new DialogInterface.OnClickListener() {
//            @Override
//            public void onRetryClick(DialogInterface dialog, int which) {
//                Tools.toSystemSetting(context);
//            }
//        });
        serverDialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(R.string.dialog_confirm), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        serverDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                serverDialog = null;
            }
        });

        try {
            serverDialog.show();
        } catch (WindowManager.BadTokenException e) {
            e.printStackTrace();
        }

    }

//    private AlertDialog locationDialog;
    /**
     * 显示未定位成功对话框
     * @param context
     */
//    public void showLocationError(final Context context) {
//        if(locationDialog !=null&&locationDialog.isShowing()){
//            MLog.d("dialog","定位失败dialog已显示");
////            try{
////                locationDialog.cancel();
////            }catch (Exception e){
////                e.printStackTrace();
////            }
////            locationDialog = null;
//            return;
//        }
//
//        locationDialog = new AlertDialog.Builder(context).create();
//        locationDialog.setTitle(R.string.get_location_fail);
//        locationDialog.setMessage(context.getResources().getString(R.string.get_location_fail_describe));
////        serverDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.set_network), new DialogInterface.OnClickListener() {
////            @Override
////            public void onRetryClick(DialogInterface dialog, int which) {
////                Tools.toSystemSetting(context);
////            }
////        });
//        locationDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.go_setting), new DialogInterface.OnClickListener() {
//            @Override
//            public void onRetryClick(DialogInterface dialog, int which) {
//                Intent intent = new Intent();
//                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
//                Uri uri = Uri.fromParts("package", context.getPackageName(), null);
//                intent.setData(uri);
//                context.startActivity(intent);
//            }
//        });
//        locationDialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
//            @Override
//            public void onRetryClick(DialogInterface dialogInterface, int i) {
//                locationDialog.cancel();
//            }
//        });
//        locationDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialog) {
//                locationDialog = null;
//            }
//        });
//
//        try {
//            locationDialog.show();
//        } catch (WindowManager.BadTokenException e) {
//            e.printStackTrace();
//        }
//
//    }

//    private AlertDialog updateDialog;
//    public void showUpdateMessageDialog(boolean cancelable, Context context, String title, String msg, String postiveBtnTxt,
//                                        DialogInterface.OnClickListener posListener, String negtiveBtnTxt, DialogInterface.OnClickListener negListener){
//        if(updateDialog !=null){
//            try{
//                updateDialog.cancel();
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//            updateDialog = null;
//        }
//
//        updateDialog= new AlertDialog.Builder(context).create();
//        updateDialog.setCancelable(cancelable);
//
//        if(!TextUtils.isEmpty(title)){
//            updateDialog.setTitle(title);
//        }
//        if(!TextUtils.isEmpty(msg)){
//            updateDialog.setMessage(msg);
//        }
//        if(!TextUtils.isEmpty(postiveBtnTxt)){
//            updateDialog.setButton(DialogInterface.BUTTON_POSITIVE, postiveBtnTxt, posListener);
//        }
//        if(!TextUtils.isEmpty(negtiveBtnTxt)){
//            updateDialog.setButton(DialogInterface.BUTTON_NEGATIVE, negtiveBtnTxt, negListener);
//        }
//
//        updateDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
//            @Override
//            public void onDismiss(DialogInterface dialog) {
//                updateDialog = null;
//            }
//        });
//
//        try {
//            updateDialog.show();
//        }catch (WindowManager.BadTokenException e){
//            e.printStackTrace();
//        }
//
//    }


}
